<?php
header("Content-Type: text/html; charset=utf-8");
require_once '../baseClasses/config_ser.php';
//require_once '../baseClasses/config_loc.php';
require_once '../baseClasses/dbConn.php';

$db = new dbConn();

$type = isset($_GET["type"]) ? htmlspecialchars($_GET["type"]) : "";
$date = isset($_GET["date"]) ? htmlspecialchars($_GET["date"]) : "";
$callback = isset($_GET["callback"]) ? htmlspecialchars($_GET["callback"]) : "";
$cateid = isset($_GET['cateid']) ? htmlspecialchars($_GET["cateid"]) : "";

//$date 的追加处理
if (!empty($date) && date('H')<10) {
	$date = date('Y-m-d',strtotime("-1 day",strtotime($date)));
}
// =====================
if($type)
{
	//$type == 1 : 今日推荐
	if($type==1 && $date)
	{
		$where = $cateid ? (" AND c2.id = ".$cateid) : '';
		$db->sql = "SELECT c1.id,c1.publishDate,c1.picture,c1.title,CONCAT(c1.goodsUrl,'&outer_code=',a.id) goodsUrl,c1.`desc`,c1.originalPrice,c1.currentPrice,c1.sort,c1.cateID,c2.cate cateName,c1.saledNum,c1.goodsID,c1.commission FROM `content` c1 LEFT JOIN `category` c2 ON c1.cateID=c2.id LEFT JOIN `admin` a ON c1.updater = a.`userName` WHERE c1.status=1 AND c1.isDel=0 AND c1.publishDate = '".trim($date)."' {$where} ORDER BY c1.sort ASC,c1.id ASC ";
		$db->doResultList2();		
	}
	
	//$type == 2 : 9.9包邮
	elseif($type==2 && $date)
	{
		$db->sql = "SELECT p.id,p.publishDate,p.picture,p.title,CONCAT(p.goodsUrl,'&outer_code=',a.id) goodsUrl,p.`desc`,p.originalPrice,p.currentPrice,p.sort,p.saledNum,p.startTime,p.endTime,p.goodsID,p.commission FROM `priceninepointnine` p LEFT JOIN `admin` a ON p.updater = a.`userName` WHERE p.`status`=1 AND p.isDel=0 AND p.publishDate = '".$date."'";
		$db->doResultList2();
	}
	
	//$type == 3 : 促销
	elseif($type==3)
	{
		$db->sql = "SELECT c1.id,c1.picture,c1.title,c1.goodsUrl,c1.sort,c1.cateID,c2.cate cateName,c1.saledNum FROM cuxiao c1 LEFT JOIN category c2 ON c1.cateID=c2.id WHERE onSale = 1 AND `status` = 1";
		$db->doResultList2();
	}
	else 
	{
		echo json_encode(array("msg"=>"error"));
		exit;
	}
	
	$data = $db->result;
	foreach ($data as $key => $val){
		if( ! preg_match("/^http\:\/\//", $val['picture']))
		{
			$data[$key]['picture'] = "http://app100656353.qzone.qzoneapp.com/".$val['picture'];
		}
	
		if($type == 2){
			$now = time();
			$data[$key]['CountDown'] = 0;
			if($now > $val['startTime'] && $now < $val['endTime']){
				$data[$key]['CountDown'] = $val['endTime'] - $now;
			}
		}
	}
	
	$result = json_encode($data);	
	echo $callback ? $callback."($result)" : $result;	
}
else
{
	echo json_encode(array("msg"=>"error"));
	exit;
}
